package marlon.smsbatcheditor.view;

import marlon.smsbatcheditor.R;
import marlon.smsbatcheditor.model.RuleAction;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 
 * @author marlonyao<yaolei135@gmail.com>
 */
public class RuleActionView extends RelativeLayout implements OnItemSelectedListener {
	// event listener class
	public interface OnRemoveListener {
		void onRemove(RuleActionView view);
	}
	
	// controls
	private Spinner spAction;
	private ViewGroup vgActionValue;
	private EditText etActionValue;
	private ImageView btnDelete;
	private ImageView btnHelp;
	
	// listeners
	private OnRemoveListener onRemoveListener;
	
	public RuleActionView(RuleAction model, Context context) {
		super(context);
		_init(model, context);
	}
	
	public RuleActionView(Context context) {
		this(null, context);
	}
	
	private void _init(RuleAction model, final Context context) {
		LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		li.inflate(R.layout.rule_action, this, true);
		
		spAction = (Spinner) findViewById(R.id.spAction);
		spAction.setAdapter(createSpinnerAdapter(R.array.array_rule_actions));
		spAction.setOnItemSelectedListener(this);
		btnDelete = (ImageView) findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (onRemoveListener != null)
					onRemoveListener.onRemove(RuleActionView.this);
			}
		});
		btnHelp = (ImageView) findViewById(R.id.btnHelp);
		btnHelp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builer = new AlertDialog.Builder(context);
	       		builer.setMessage("You can use $ADDRESS$/$BODY$ to represent the original SMS address/body.")
	       			.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
	       		builer.create().show();
			}
		});
		
		vgActionValue = (ViewGroup) findViewById(R.id.vgActionValue);
		etActionValue = (EditText) findViewById(R.id.etActionValue);
		
		if (model != null) {
			RuleAction.Type type = model.getType();
			spAction.setSelection(type.ordinal());
			if (model.getValue() != null)
				etActionValue.setText(model.getValue());
		}
		updateView();
	}
	
	private ArrayAdapter<CharSequence> createSpinnerAdapter(int arrResId) {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), arrResId, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		return adapter;
	}
	
	private boolean actionValueVisible() {
		return spAction.getSelectedItemPosition() > 2;
	}
	
	private String getActionValue() {
		if (actionValueVisible())
			return etActionValue.getText().toString();
		return null;
	}
	
	public RuleAction getRuleAction() {
		return new RuleAction(RuleAction.Type.values()[spAction.getSelectedItemPosition()],
				getActionValue());
	}
	
	private void updateView() {
		if (actionValueVisible()) {
			vgActionValue.setVisibility(VISIBLE);
			if (spAction.getSelectedItemPosition() == RuleAction.Type.MODIFY_ADDRESS.ordinal()) {
				etActionValue.setInputType(InputType.TYPE_CLASS_PHONE);
			} else {
				etActionValue.setInputType(InputType.TYPE_CLASS_TEXT);
			}
		} else {
			vgActionValue.setVisibility(GONE);
		}
	}

	public boolean validate() {
		if (actionValueVisible() && getActionValue().length() == 0) {
			Toast.makeText(getContext(), "Must specify action value", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		updateView();
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// OnItemSelectedListener interface, do nothing
	}
	
	public void setOnRemoveListener(OnRemoveListener listener) {
		this.onRemoveListener = listener;
	}
}
